package zemberek3.crosscheck;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.io.Resources;
import smoothnlp.core.io.SimpleTextReader;
import smoothnlp.core.io.SimpleTextWriter;
import smoothnlp.core.io.Strings;
import zemberek3.apps.TurkishMorphParser;
import zemberek3.lexicon.DictionaryItem;
import zemberek3.lexicon.RootLexicon;
import zemberek3.lexicon.tr.TurkishDictionaryLoader;
import zemberek3.lexicon.tr.TurkishSuffixes;
import zemberek3.parser.morphology.MorphParse;
import zemberek3.shared.lexicon.PrimaryPos;

import java.io.File;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

public class P3sgCompounds {
    public static void main(String[] args) throws IOException {

        // extract();
        fix();
    }

    private static void fix() throws IOException {
        List<String> all = SimpleTextReader.trimmingUTF8Reader(new File(Resources.getResource("tr/non-tdk.dict").getFile())).asStringList();
        List<String> comp = SimpleTextReader.trimmingUTF8Reader(new File(Resources.getResource("tr/no-tdk-compounds_.txt").getFile())).asStringList();
        Map<String, String> compMap = Maps.newHashMap();
        for (String s : comp) {
            compMap.put(s.split("[ ]")[0], s);
        }
        SimpleTextWriter stw = SimpleTextWriter.keepOpenUTF8Writer(new File(Resources.getResource("tr/non-tdk2.txt").getFile()));
        for (String s : all) {
            String s1 = s.split("[ ]")[0];
            if (compMap.containsKey(s1))
                stw.writeLine(compMap.get(s1));
            else
                stw.writeLine(s);
        }
        stw.close();
    }

    private static void extract() throws IOException {
        TurkishMorphParser parser = TurkishMorphParser.createWithDefaults();

        RootLexicon lexicon = new TurkishDictionaryLoader(new TurkishSuffixes()).load(new File(Resources.getResource("tr/non-tdk.dict").getFile()));

        LinkedHashSet<String> compounds = Sets.newLinkedHashSet();
        LinkedHashSet<String> rest = Sets.newLinkedHashSet();

        for (DictionaryItem item : lexicon) {
            String line = item.lemma;
            if (line.contains("CompoundP3sg")) {
                compounds.add(line);
                continue;
            }
            String root = line;
            if (line.contains(" "))
                root = Strings.subStringUntilFirst(line, " ");
            if (root.length() < 6) {
                rest.add(line);
                continue;
            }

            boolean found = false;
            for (int i = 2; i < root.length() - 2; i++) {
                String first = root.substring(0, i);
                String second = root.substring(i);

                List<MorphParse> result1 = parser.parse(first);
                if (result1.size() == 0)
                    continue;
                List<MorphParse> result2 = parser.parse(second);
                if (result2.size() == 0)
                    continue;

                for (MorphParse result : result2) {
                    if (result.getPos() != PrimaryPos.Verb && result.formatNoEmpty().contains("P3sg")) {
                        if (result.getPos() == PrimaryPos.Noun)
                            compounds.add(root + " [A:CompoundP3sg, Ext; Roots:" + first + "-" + result.dictionaryItem.root + "]");
                        else
                            compounds.add(root + " [P: " + item.primaryPos.shortForm + "; A:CompoundP3sg, Ext; Roots:" + first + "-" + result.dictionaryItem.root + "]");
                        found = true;
                    }
                }
            }
            if (!found)
                rest.add(line);

            SimpleTextWriter.oneShotUTF8Writer(new File(Resources.getResource("tr/no-tdk-regular.txt").getFile())).writeLines(rest);
            SimpleTextWriter.oneShotUTF8Writer(new File(Resources.getResource("tr/no-tdk-compounds.txt").getFile())).writeLines(compounds);
        }
    }
}
